package com.lzhl.buildjar.ui;

import com.lzhl.buildjar.ui.support.BaseDialog;
import com.lzhl.buildjar.util.FileUtils;

import javax.swing.*;
import java.awt.*;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/4/25
 */
public class HelpUI extends BaseDialog {
    private MainUI mainUI;
    private JScrollPane scrollPane;
    private JTextPane textPane;

    public HelpUI(Frame owner) {
        super(owner, "帮助", true);

        this.mainUI = (MainUI)owner;

        jbinit();
        init();

        int width = (int) (owner.getSize().width * 0.8);
//          int height = (int) (owner.getSize().height * 0.6);
        setSize(width, 400);
        setLocationRelativeTo(owner);
        setVisible(true);
    }

    private void jbinit() {
        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        textPane = new JTextPane();
        textPane.setEditable(false);
        scrollPane = new JScrollPane(textPane);

        container.add(scrollPane, BorderLayout.CENTER);
    }

    private void init() {
        try {
            java.util.List<String> lines = FileUtils.readTextFile("readme.txt", true);

            StringBuffer text = new StringBuffer();
            for(String line : lines) {
                text.append(line).append("\n");
            }

            textPane.setText(text.toString());
            textPane.setSelectionStart(0);
            textPane.setSelectionEnd(0);
        }
        catch (Exception e) {
            textPane.setText("帮助文件打开失败。");
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                scrollPane.getVerticalScrollBar().setValue(0);
            }
        });
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ignore) {
        }

        new HelpUI(null);
    }
}